package com.goods.business.service.imp;

import com.goods.business.converter.SupplierConverter;
import com.goods.business.mapper.SupplierMapper;
import com.goods.business.service.SupplierService;
import com.goods.common.error.BusinessCodeEnum;
import com.goods.common.error.BusinessException;
import com.goods.common.model.business.Supplier;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.Date;
import java.util.List;

@Service
public class SupplierServiceImpl implements SupplierService {
    @Resource
    private SupplierMapper supplierMapper;

    //查询物资的列表 分页带查询条件
    @Override
    public PageVO<SupplierVO> findSupplierList(Integer pageNum, Integer pageSize,SupplierVO supplierVO) {
        //List<Supplier> supplierList = supplierMapper.selectAll();

        String address = supplierVO.getAddress();
        String contact = supplierVO.getContact();
        Example example = new Example(Supplier.class);
        if (address!=null&&!"".equals(address)){
            example.createCriteria().andEqualTo("address",address);
        }
        if (contact!=null&&!"".equals(contact)){
            example.createCriteria().andLike("contact","%"+contact+"%");
        }
        List<Supplier> suppliers = supplierMapper.selectByExample(example);
        List<SupplierVO> supplierVOList = SupplierConverter.converterToSupplierVOList(suppliers);
        List<Supplier> page = ListPageUtils.page(suppliers, pageSize, pageNum);
        return new PageVO<>(page.size(),supplierVOList);
    }
    //添加物资来源信息
    @Override
    public void add(SupplierVO supplierVO) {
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierVO,supplier);
        supplier.setCreateTime(new Date());
        supplier.setModifiedTime(new Date());

        supplierMapper.insert(supplier);
    }
    //编辑物资来源
    @Override
    public SupplierVO edit(Long id) {
        Supplier supplier = supplierMapper.selectByPrimaryKey(id);
        SupplierVO supplierVO = SupplierConverter.converterToSupplierVO(supplier);
        return supplierVO;
    }
    //修改物资来源
    @Override
    public void update(Long id,SupplierVO supplierVO) throws BusinessException {
        Supplier supplier = supplierMapper.selectByPrimaryKey(id);
        if (supplier == null){
            throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"数据不存在!");
        }else {
            BeanUtils.copyProperties(supplierVO,supplier);
            supplier.setModifiedTime(new Date());
            supplier.setId(id);
            supplierMapper.updateByPrimaryKey(supplier);
        }
    }
    //删除
    @Override
    public void delete(Long id) {
        Supplier supplier = supplierMapper.selectByPrimaryKey(id);
        supplierMapper.delete(supplier);
    }
    //根据物资来源的id获取物资的信息
    @Override
    public Supplier getBySupplierId(Long id) {
        return supplierMapper.selectByPrimaryKey(id);
    }
    //获取所有的物资信息来源
    @Override
    public List<SupplierVO> findAll() {
        List<Supplier> suppliers = supplierMapper.selectAll();
        List<SupplierVO> supplierVOList = SupplierConverter.converterToSupplierVOList(suppliers);
        return supplierVOList;
    }
}
